package com.example.demo.controller;

import com.example.demo.pojo.Rule;
import com.example.demo.service.RuleService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import com.example.demo.pojo.Result;
import java.util.List;

@RestController
@RequestMapping("/rule")
@CrossOrigin(origins = "http://localhost:5173")
public class RuleController {

    @Resource
    private RuleService ruleService;

    @PostMapping("/addRule")
    public Result saveRule(@RequestBody Rule rule) {
        if (rule.getRuleId() == null) {
            return Result.error("ruleId 不能为空");
        }
        ruleService.saveRule(rule);
        return Result.success();
    }

    @PutMapping("/updateRule")
    public Result<String> updateRule(@RequestBody Rule rule) {
        ruleService.updateRule(rule);
        return Result.success();
    }

    @DeleteMapping("/{ruleId}")
    public Result<String> deleteRule(@PathVariable Integer ruleId) {
        ruleService.deleteRule(ruleId);
        return Result.success();
    }

    @GetMapping("/getAllRules")
    public Result<List<Rule>> getAllRules() {
        List<Rule> rules = ruleService.getAllRules();
        return Result.success(rules);
    }
}